package com.example.zhihu.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.zhihu.entity.HotInfo;
import com.example.zhihu.service.IHotInfoService;
import com.example.zhihu.vo.HotInfoVo;
import com.example.zhihu.vo.HotInfoVo2;
import com.example.zhihu.vo.TopicsDataVo;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-04-17
 */
@RestController
@RequestMapping("/hot-info")
public class HotInfoController {

  @Resource
  IHotInfoService iHotInfoService;

  /**
   * 获取数据库中的所有爬取数据
   * @return
   */
  @GetMapping("/list")
  public R<List<HotInfo>> list() {
    return R.ok(iHotInfoService.list());
  }

  /**
   * 获取数据库的前10条数据，官方排名
   * @param count
   * @return
   */
  @GetMapping("/first")
  public R<List<HotInfoVo>> first(Integer count, String currentDate) {
    List<HotInfo> hotInfoList = iHotInfoService.first(count, currentDate);
    List<HotInfoVo> hotInfoVoList = iHotInfoService.addUnusual(hotInfoList);
    return R.ok(hotInfoVoList.stream().sorted(((o1, o2) -> o2.getHotIndex().compareTo(o1.getHotIndex()))).collect(
        Collectors.toList()));
  }

  /**
   * 实际排名
   * @param count
   * @return
   */
  @GetMapping("/realFirst")
  public R<List<HotInfoVo>> realFirst(Integer count, String currentDate) {
    List<HotInfo> hotInfoList = iHotInfoService.first(count, currentDate);
    List<HotInfoVo> hotInfoVoList = iHotInfoService.addUnusual(hotInfoList);
    return R.ok(hotInfoVoList);
  }

  /**
   * 查询某话题今天的数据变化情况
   * @param infoId
   * @return
   */
  @GetMapping("/today")
  public R<List<HotInfoVo2>> listByInfoId(String infoId, String currentDate) {
    LocalDateTime dateTime = LocalDateTimeUtil.parse(currentDate, "yyyy-MM-dd");
    List<HotInfo> hotInfoList = iHotInfoService.listByInfoId(infoId, dateTime);
    List<HotInfoVo2> hotInfoVo2List = iHotInfoService.convert2Vo2(hotInfoList);
    return R.ok(hotInfoVo2List);
  }

  /**
   * 某个话题的数据变化量（小时）
   * @param infoId
   * @return
   */
  @GetMapping("/hour")
  public R<TopicsDataVo> listByInfoIdHour(String infoId, String currentDate) {
    LocalDateTime dateTime = LocalDateTimeUtil.parse(currentDate, "yyyy-MM-dd");
    List<HotInfo> hotInfoList = iHotInfoService.listByInfoId(infoId, dateTime);
    List<HotInfoVo2> hotInfoVo2List = iHotInfoService.convert2Vo2(hotInfoList);
    TopicsDataVo topicsDataVo = new TopicsDataVo();
    HotInfoVo2 vo1 = hotInfoVo2List.get(hotInfoVo2List.size() - 1);
    HotInfoVo2 vo2 = hotInfoVo2List.get(hotInfoVo2List.size() - 2);
    topicsDataVo.setInfoId(vo1.getInfoId());
    topicsDataVo.setTitle(vo1.getTitle());
    topicsDataVo.setAnswerNum(vo1.getAnswerCount());
    topicsDataVo.setCommentNum(vo1.getCommentCount());
    topicsDataVo.setFollowNum(vo1.getFollowerCount());
    topicsDataVo.setVisitNum(vo1.getVisitCount());
    topicsDataVo.setNewAnswerNum(vo1.getAnswerCount() - vo2.getAnswerCount());
    topicsDataVo.setNewCommentNum(vo1.getCommentCount() - vo2.getCommentCount());
    topicsDataVo.setNewFollowNum(vo1.getFollowerCount() - vo2.getFollowerCount());
    topicsDataVo.setNewVisitNum(vo1.getVisitCount() - vo2.getVisitCount());

    return R.ok(topicsDataVo);
  }


}
